const path = require('path')
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer")
const TerserPlugin = require('terser-webpack-plugin')
const entrys = require('./entrys.js')
const htmlArray = require('./htmlArray.js')

const otherPlugins = [
  // new BundleAnalyzerPlugin(),
  new CleanWebpackPlugin(),
  new ExtractTextPlugin("[name].css"),
  new CopyWebpackPlugin([{
    from: path.resolve(__dirname, "./src/assets"),
    to: './assets',
    ignore: ['.*']
  }])
]


module.exports = {
  entry: entrys,
  mode: 'development',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/[name].min.js',
    publicPath: ''
  },
  devServer: {
    contentBase: "./dist/",
    historyApiFallback: true,
    disableHostCheck: true,
    inline: true,
    hot: true,
    host: '127.0.0.1',
    port: 8088
  },
  resolve: {
    alias: {
      "~": path.resolve("src/")
    },
    extensions: ['.js', '.less', '.css'],
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: "style-loader!css-loader",
      },
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
          test: /\.js$/,
          loader: 'babel',
          exclude: /(node_modules|src)/,
          query: {
              presets: ['es2015']
          }
      }
    ]
  },
  plugins: [...htmlArray, ...otherPlugins],
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin()]
  }
}